package com.apes.fn.fico.reimbursement.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.scm.merchant.disbursement.model.DisbursementItem;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 赋能费用类型
 * @author cyy
 * @create 2023.3.28
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name= FeeTypeExpense.CONST_TABLE_NAME)
public class FeeTypeExpense extends Model {
    public static final String CONST_TABLE_NAME = "fn_fee_type_expense";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "编码",prefix = "FF", padding = 5)
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_Date")
    @Comment("审核时间")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "approve", name = "审核"),
            @State(id = "delete", name = "删除")
    })
    private String state;

    @Column(name = "amount_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("费用总额")
    private double amountTotal= 0;

    @OneToMany(mappedBy = "feeTypeExpense", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<FeeTypeExpenseItem> feeTypeExpenseItems = new ArrayList();

    public List<FeeTypeExpenseItem> getFeeTypeExpenseItems() {
        for (FeeTypeExpenseItem item : feeTypeExpenseItems) {
            item.setFeeTypeExpense(this);
        }
        return feeTypeExpenseItems;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
